package com.wjh.common;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.RandomStringUtils;

/**
 * 公共方法类
 * 
 * @author wenjianhai
 * @date 2023/6/21
 * @since JDK 17
 */
public final class CommonMethods {
	private CommonMethods() {
	}

	/**
	 * 获取UUID
	 *
	 * @return
	 */
	public static String getMeUUID() {
		String strs = UUID.randomUUID().toString().replace("-", "");
		int intRandom = Math.abs(UUID.randomUUID().hashCode());
		String strtmp = "0a1b2c3d4e5f6a7b8c9d" + intRandom + "9f8e7d6c5b4a3f2e1d0c" + intRandom;// 初始化种子
		strs = RandomStringUtils.random(4, strtmp) + strs;// 返回intLen为的字符串
		return strs.toLowerCase();
	}

	public static String getNumber(int intLen) {
		int intRandom = Math.abs(UUID.randomUUID().hashCode());
		String strs = "0123456789" + intRandom + "9876543210" + intRandom;// 初始化种子
		return RandomStringUtils.random(intLen, strs);// 返回intLen为的字符串
	}

	/**
	 * 元转分
	 *
	 * @date 2023-06-22
	 * @param yuan 元
	 * @return long
	 * @since JDK 17
	 * @author wenjianhai
	 */
	public static long yuanToFen(BigDecimal yuan) {
		if (yuan == null) {
			return 0;
		}
		return yuan.multiply(new BigDecimal(Constants.STR_ONE_HUNDRED)).longValue();
	}

	/**
	 * 分转元
	 *
	 * @date 2022-05-27
	 * @param fen 分
	 * @return BigDecimal
	 * @since JDK 1.8
	 * @author wenjianhai
	 */
	public static BigDecimal fenToYuan(long fen) {
		return new BigDecimal(fen).divide(Constants.PERCENT, Constants.AMOUNT_SCALE, RoundingMode.HALF_DOWN);
	}

	public static Map<String, String> requestParamsToMap(Map<String, String[]> requestParams) {
		if (MapUtils.isEmpty(requestParams)) {
			return null;
		}
		Map<String, String> map = new HashMap<>();

		for (String name : requestParams.keySet()) {
			String[] values = (String[]) requestParams.get(name);
			String valueStr = "";
			for (int i = 0; i < values.length; i++) {
				valueStr = (i == values.length - 1) ? valueStr + values[i]
						: valueStr + values[i] + ",";
			}
			map.put(name, valueStr);
		}
		return map;
	}
}
